<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\TemplateModel;
use app\common\model\TemplateAttrModel;

class TemplateController extends AdminBaseController
{
    protected $targets = ["_blank" => "新标签页打开", "_self" => "本窗口打开"];

    /**
     * 模板管理管理
     * @adminMenu(
     *     'name'   => '模板管理',
     *     'parent' => 'admin/Setting/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 50,
     *     'icon'   => '',
     *     'remark' => '模板管理管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $TemplateModel = new TemplateModel();
        $templates     = $TemplateModel->select();
        $this->assign('templates', $templates);

        return $this->fetch();
    }

    /**
     * 添加模板管理
     * @adminMenu(
     *     'name'   => '添加模板管理',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加模板管理',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $this->assign('targets', $this->targets);
        return $this->fetch();
    }

    /**
     * 添加模板管理提交保存
     * @adminMenu(
     *     'name'   => '添加模板管理提交保存',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加模板管理提交保存',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        $data      = $this->request->param();
		$data['addtime']=time();
        $TemplateModel = new TemplateModel();
        $result    = $TemplateModel->validate(true)->allowField(true)->save($data);
        if ($result === false) {
            $this->error($TemplateModel->getError());
        }

        $this->success("添加成功！", url("template/index"));
    }

    /**
     * 编辑模板管理
     * @adminMenu(
     *     'name'   => '编辑模板管理',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑模板管理',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id        = $this->request->param('id', 0, 'intval');
		$TemplateModel = new TemplateModel();
        $Template = $TemplateModel->get($id);
        $this->assign('targets', $this->targets);
        $this->assign('template', $Template);
        return $this->fetch();
    }

    /**
     * 编辑模板管理提交保存
     * @adminMenu(
     *     'name'   => '编辑模板管理提交保存',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑模板管理提交保存',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $data      = $this->request->param();
        $TemplateModel = new TemplateModel();
		
        $result    = $TemplateModel->validate(true)->allowField(true)->isUpdate(true)->save($data);
        if ($result === false) {
            $this->error($TemplateModel->getError());
        }

        $this->success("保存成功！", url("template/index"));
    }

    /**
     * 删除模板管理
     * @adminMenu(
     *     'name'   => '删除模板管理',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除模板管理',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        LinkModel::destroy($id);

        $this->success("删除成功！", url("template/index"));
    }

    /**
     * 模板管理排序
     * @adminMenu(
     *     'name'   => '模板管理排序',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '模板管理排序',
     *     'param'  => ''
     * )
     */
    public function listOrder()
    {
        $TemplateModel = new  TemplateModel();
        parent::listOrders($TemplateModel);
        $this->success("排序更新成功！");
    }

    /**
     * 模板管理显示隐藏
     * @adminMenu(
     *     'name'   => '模板管理显示隐藏',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '模板管理显示隐藏',
     *     'param'  => ''
     * )
     */
    public function toggle()
    {
        $data      = $this->request->param();
        $TemplateModel = new TemplateModel();

        if (isset($data['ids']) && !empty($data["display"])) {
            $ids = $this->request->param('ids/a');
            $TemplateModel->where(['id' => ['in', $ids]])->update(['status' => 1]);
            $this->success("更新成功！");
        }

        if (isset($data['ids']) && !empty($data["hide"])) {
            $ids = $this->request->param('ids/a');
            $TemplateModel->where(['id' => ['in', $ids]])->update(['status' => 0]);
            $this->success("更新成功！");
        }


    }
	public function updatestatus()
    {
        $data      = $this->request->param();
        $TemplateModel = new TemplateModel();

        if (isset($data['ids']) && !empty($data["display"])) {
            $ids = $this->request->param('ids/a');
            $TemplateModel->where(['id' => ['in', $ids]])->update(['status' => 1]);
            $this->redirect('Template/index');
        }

        if (isset($data['ids']) && !empty($data["hide"])) {
            $ids = $this->request->param('ids/a');
            $TemplateModel->where(['id' => ['in', $ids]])->update(['status' => 0]);
            $this->redirect('Template/index');
        }


    }
	//显示课题步骤
	public function progresslist(){
		$data      = $this->request->param();		
		$list=db('classprogress')->where(['mid'=>$data['id']])->select();
		$this->assign('mid', $data['id']);
		$this->assign('progressid', $data['progressid']);		
		$this->assign('list', $list);
		return $this->fetch();
	}
	//添加课题步骤
	public function addclass_progress(){
		$data      = $this->request->param();
		$this->assign('data', $data);
		return $this->fetch();
	}
	//提交课题步骤
	public function addclass_progresspost(){
		$data      = $this->request->param();
		$result=db('classprogress')->insert($data);
		if ($result === false) {
            $this->error($TemplateModel->getError());
        }
        $this->success("添加成功！", url("template/index"));
	}
	public function delclass_progress(){
		$id = $this->request->param('id', 0, 'intval');
        db('classprogress')->delete($id);
        $this->success("删除成功！");		
	}
	//显示课题步骤
	public function look(){
		$data      = $this->request->param();		
		$list=db('classprogress')->where(['mid'=>$data['id']])->select();
        for ($i=1; $i <= $data['progressid'] ; $i++) { 
            $name[$i] = TemplateAttrModel::where('template_id',$data['id'])->where('progress',$i)->value('name');
        }
		$this->assign('mid', $data['id']);
		$this->assign('progressid', $data['progressid']);		
		$this->assign('list', $list);
        $this->assign('name', $name);
		return $this->fetch();
	}
}